﻿package code
{
	import flash.display.*;
	import gemini.utils.*;
	import flash.utils.*
	public class DeskModule extends MovieClip
	{
		public var mainframe;
		public var mod;
		public var ssx;
		public var ssy;
		public var clip="clip";
		public var pinned;
		var onComplete;
		var lscale;
		public var delay = 0;
		/**
		 * DeskModule is a class that is a container for loaded SWF module
		 * @param	mf
		 */
		public function DeskModule(mf:Core)
		{
			mainframe = mf;
			onComplete=[];
		}
		public function addOnComplete(fnc)
		{
			onComplete.push(fnc);
		}
		public function init()
		{
			trace("starting module load ");
			lscale=new moduleloadingscale();
			addChild(lscale);
			pinned=mod.pin;
			var to=setTimeout(startLoading,delay);
			
			
		}
		private function startLoading()
		{
			trace(this+" started loading");
			var bl = new BulkLoader();

			bl.addFile("modules/" + mod.clip,this,"clip");
			bl.onComplete(addMod);
			bl.onProgress(setLoadScale);
			bl.loadFiles();
		}
		private function setLoadScale(e)
		{
			//trace("loading module progress "+e);
			var perc=(Math.round((e.loadedsize/e.totalsize)*100));
			lscale.scl.gotoAndStop(perc);
			
		}
		private function addMod(e)
		{
			trace("module loaded "+clip.title.eng);
			removeChild(lscale);
			lscale=null;
			clip.x =  -  clip.width / 2;
			clip.y =  -  clip.height / 2;
			clip.mainframe=mainframe
			for (var prop in mod.params)
			{
				trace(prop);
				clip[prop] = mod.params[prop];
			}
			try
			{
				clip.init();
			}
			catch (e)
			{

			}
			addChildAt(clip,0);
			mainframe.makeBackground();
			
			
			for(var n=0;n<onComplete.length;n++)
			{
				onComplete[n](this);
			}
		}

	}

}